#include "rrprojectivetrans.h"
#include <cmath>

RRProjectiveTrans::RRProjectiveTrans()
{
    fovy = 38.0f;
    aspect = 640.0f / 480.0f;
    near = 1.0f;
    far = 500.0f;
}

void RRProjectiveTrans::rrSetPerspective(float fFov, float fAspect, float fNear, float fFar)
{
    fovy = fFov;
    aspect = fAspect;
    near = fNear;
    far = fFar;
}

void RRProjectiveTrans::rrProjectiveTrans(RRPrim *arg)
{
    RRVertex *vp;
    int vnum;
    float Sy, Sx, Sz;
    vnum = arg->rrGetVertsNum();
    for(int i=0; i<vnum; i++) {
        vp = arg->rrGetVertAdrs(i);
        Sy = tanf(fovy * 3.14159 / 360.0) * vp->z;
        Sx = Sy * aspect;
        Sz = far / (far - near);
        vp->w = 1.0;
        vp->x = vp->x / Sx;
        vp->y = vp->y / Sy;
        vp->z = Sz * (vp->z - near);
    }
}
